#include "mainwindow.h"


/*
setValve参数说明：
    channel_num： 取值1,2,3,4,5,6,7,8  分别代表8个阀
    valve_status：取值只能为1或0,  1代表开阀，0代表关阀

*/
void MainWindow::setValve(quint8 channel_num, quint16 valve_status)
{
   // uint16_t valve_addr_1 = 1544;
    quint16 addr = addr_valve_NO_1+channel_num-1;

    mutex_midway->lock();
    sendWriteHoldingReg(SerDev_Mid, MIDWAY_MB_SLAVE_ID, addr, valve_status);
    mutex_midway->unlock();
    //qDebug()<<"setValve"<<endl;
    delay(250);
}


/*
setEPC参数说明：
    channel_num： 取值0,1,2  分别代表EPC的3个通道
    setValue：    单位是kPa, 例如输入300 就是300kPa

*/
void MainWindow::setEPC(quint8 channel_num, float setValue)
{
    if(channel_num>2)
    {
       qDebug()<<"setEPC's channel input error"<<endl;
       //ui->TxtLog->append("["+QTime::currentTime().toString()+"]"+"->setEPC's channel input error");
       Log("->setEPC's channel input error");
       return;
    }
    mutex_epcmfc->lock();
    sendWriteFloatReg(SerDev_EPC_MFC, EPC_MB_SLAVE_ID, (addr_EPC_Press_channel_1+channel_num*4), setValue);
    mutex_epcmfc->unlock();
    qDebug()<<"setEPC"<<endl;
    delay(250);
}

/*
ReadEPC 参数说明：
    EPC_MODE:     0-> press mode  1: Flow mode


*/
void MainWindow::ReadEPC(quint8 EPC_mode)
{
    quint16 EPCFLOW_START_ADDR = 0x00;

    switch(EPC_mode)
    {
        case 0: //Press mode
        {
            EPCFLOW_START_ADDR = 0;

            glabalInfo.epcInfo.epc_press[0] = 0;
            glabalInfo.epcInfo.epc_press[1] = 0;
            glabalInfo.epcInfo.epc_press[2] = 0;

            qDebug()<<"--------EPC press mode"<<endl;

            uint8_t ret = sendReadMultiFloatReg(SerDev_EPC_MFC,EPC_MB_SLAVE_ID,glabalInfo.epcInfo.epc_press, EPCFLOW_START_ADDR, 3);
            if(ret<0)
            {
                //ui->TxtLog->append("["+QTime::currentTime().toString()+"]"+" ReadEPC Fail ");
                Log(" ReadEPC Fail ");
            }
            break;
        }
        case 1: //FLOW mode
        {
            EPCFLOW_START_ADDR = 6;

            glabalInfo.epcInfo.epc_flow[0] = 0;
            glabalInfo.epcInfo.epc_flow[1] = 0;
            glabalInfo.epcInfo.epc_flow[2] = 0;

            qDebug()<<"-----EPC flow mode"<<endl;

            uint8_t ret = sendReadMultiFloatReg(SerDev_EPC_MFC,EPC_MB_SLAVE_ID,glabalInfo.epcInfo.epc_flow, EPCFLOW_START_ADDR, 3);
            if(ret<0)
            {
                //ui->TxtLog->append("["+QTime::currentTime().toString()+"]"+" ReadEPC Fail ");
                Log(" ReadEPC Fail ");
            }
            break;
        }
        default:
        {
            //ui->TxtLog->append("["+QTime::currentTime().toString()+"]"+" ReadEPC(quint8 EPC_mode)'s input param error");
            Log(" ReadEPC(quint8 EPC_mode)'s input param error");
            break;
        }
    }
    delay(1000); //Wait fot read finish
}
void MainWindow::ReadMFC()
{
    glabalInfo.MFC_value = 0;

    uint16_t cur_flow = 0;
    uint16_t ADDR_READ_CUR_FLOW = 0x01;
    cur_flow = sendReadHoldingReg(SerDev_EPC_MFC,MFC_MB_SLAVE_ID,ADDR_READ_CUR_FLOW);
    float mfc_flow = (float)cur_flow / 10;
    glabalInfo.MFC_value = mfc_flow;
    Log(QString::number(mfc_flow) + "sccm");
    delay(500); //Wait fot read finish
}

/*
setMFC参数说明：
    numOf_sccm: MFC要设置的流量值，例如 setMFC(100); 代表设置MFC为 100 sccm

*/
void MainWindow::setMFC(quint16 numOf_sccm)
{
    quint16 SERVER_ADDR = 0x31;
    numOf_sccm *= 10; //0-5000 数字量 对应 0~500 sccm

    mutex_epcmfc->lock();
    sendWriteHoldingReg(SerDev_EPC_MFC,MFC_MB_SLAVE_ID,SERVER_ADDR, numOf_sccm);
    mutex_epcmfc->unlock();

    qDebug()<<"setMFC"<<endl;
    delay(250);
}

/*
setTEC参数说明：
    channel_num： 取值0,1  TEC-0 和 TEC-1
    temperature： TEC要设置的目标温度值

*/
void MainWindow::setTEC(quint8 channel_num, float temperature)
{
    QString TEC_temp = "$R0=";
    TEC_temp += QString::number(temperature,'f',2);
    TEC_temp += "\r\n";
    QByteArray ba = TEC_temp.toLatin1();
    char *str_setTemp = ba.data();
    int ret;

    if(channel_num==0)
        mutex_tec_1->lock();
    else if(channel_num==1)
        mutex_tec_2->lock();
    else
    {
        qDebug()<<"TEC channel num wrong"<<endl;
        Log("TEC channel_num input wrong");
        return;
    }

    if((ret=serialWirte((SerDev_TEC_1+channel_num), str_setTemp, strlen(str_setTemp)))==0)
    {
        //MainWindow_log(str_setTemp);
        QString str = "Set Trap to "+ QString::number(temperature,'f',2)+" °C";
        Log(str);
        //ui->TxtLog->append("["+QTime::currentTime().toString()+"]"+"Set Trap to "+ QString::number(temperature,'f',2)+" °C");
    }

    if(channel_num==0)
        mutex_tec_1->unlock();
    else if(channel_num==1)
        mutex_tec_2->unlock();
}
void MainWindow::setTEC(quint8 channel_num, QString temperature) //temperature入参字符串重载
{
    QString TEC_temp = "$R0=";
    TEC_temp += temperature;
    TEC_temp += "\r\n";
    QByteArray ba = TEC_temp.toLatin1();
    char *str_setTemp = ba.data();
    int ret;

    if(channel_num==0)
        mutex_tec_0->lock();
    else if(channel_num==1)
        mutex_tec_1->lock();
    else if(channel_num==2)
        mutex_tec_2->lock();
    else
    {
        qDebug()<<"TEC channel num wrong"<<endl;
        Log("TEC channel_num input wrong");
        return;
    }

    if((ret=serialWirte((SerDev_TEC_0+channel_num), str_setTemp, strlen(str_setTemp)))==0)
    {
        //MainWindow_log(str_setTemp);
        QString str = "Set Trap to "+ temperature+" °C";
        Log(str);
        //ui->TxtLog->append("["+QTime::currentTime().toString()+"]"+"Set Trap to "+ QString::number(temperature,'f',2)+" °C");
    }

    if(channel_num==0)
        mutex_tec_0->unlock();
    else if(channel_num==1)
        mutex_tec_1->unlock();
    else if(channel_num==2)
        mutex_tec_2->lock();
}


/*
         HotLine
    channel_num:0:Transfer HotLine 和 1:GC HotLine
    target_temperature： 一般220C
*/
void MainWindow::setHeatingTube(quint8 channel_num, float target_temperature)
{
    uint16_t temp_addr = 0;

    switch(channel_num)
    {
      case 0: //Eject channel of Midway board HotLine [Transfer HotLine]
        temp_addr = addr_Mid_targetTransferSetTemp;
        break;

      case 1: //Sample-in channel of Midway board Hotline [GC HotLine]
        temp_addr = addr_Mid_targetGCSetTemp;
        break;
      default:
        Log("setHeatingTube's channel_num input error");
        return;
    }

    mutex_midway->lock();
    sendWriteFloatReg(SerDev_Mid, MIDWAY_MB_SLAVE_ID, temp_addr, target_temperature);
    mutex_midway->unlock();
    QString str = "HeatingTube-> set temp: "+QString::number(target_temperature,'f',2)+  "°C , addr: "+QString::number(temp_addr);
    Log(str);
    //ui->TxtLog->append("["+QTime::currentTime().toString()+"]"+"HeatingTube-> set temp: "+QString::number(target_temperature,'f',2)+  "°C , addr: "+QString::number(temp_addr));
}

/*
setHeater参数说明：
    channel_num:    取值0,1,2,3  分别代表0号热脱附加热丝和1号热脱附加热丝
    num_of_seconds: 要设置的加热丝加热时间（秒）


*/
void MainWindow::setHeater(quint8 channel_num, float num_of_seconds)
{
    uint16_t sendBuf[2];
    sendBuf[0] = 1;
    sendBuf[1] = (uint16_t)(num_of_seconds*1000/50);
    mutex_midway->lock();
    sendWriteMultiHoldingReg(SerDev_Mid,0x01,sendBuf, (addr_Mid_Heater0_UMB_ON+channel_num*2), 2);
    mutex_midway->unlock();
    qDebug()<<"setHeater"<<endl;
}


void MainWindow::setGC(bool ON_OFF)
{
    quint16 addr_GC_Triger_In = 1544;
    mutex_epcmfc->lock();
    if(ON_OFF)
    {
        sendWriteHoldingReg(SerDev_EPC_MFC, EPC_MB_SLAVE_ID, addr_GC_Triger_In, 1);
        qDebug()<<"GC ON"<<endl;
    }
    else
    {
        sendWriteHoldingReg(SerDev_EPC_MFC, EPC_MB_SLAVE_ID, addr_GC_Triger_In, 0);
        qDebug()<<"GC Off"<<endl;
    }
    mutex_epcmfc->unlock();
}





